package com.wst.lambdaa;

import java.util.Arrays;
import java.util.List;
import java.util.function.Predicate;

/**
 * @Description: Lambda表达式示例服务
 * @Auth: yudong
 * @DateTime: 2021/4/12 16:47
 */
public class LambdaDemo {

    public static void main(String[] args) {
        samplePredicateTest();
    }

    /**
     * 简单的Predicate示例
     */
    public static void samplePredicateTest(){
        List languages = Arrays.asList("Java", "Scala", "C++", "Haskell", "Lisp");

        System.out.println("Languages which starts with J :");
//        filter(languages, (str) -> str.toString().startsWith("J"));
//
//        System.out.println("Languages which ends with a ");
//        filter(languages, (str)->str.toString().endsWith("a"));
//
//        System.out.println("Print all languages :");
//        filter(languages, (str)->true);
//
//        System.out.println("Print no language : ");
//        filter(languages, (str)->false);
//
//        System.out.println("Print language whose length greater than 4:");
//        filter(languages, (str)->str.toString().length() > 4);
    }


    /**
     * Stream API 的filter方法能够接受Predicate参数, 能够允许测试多个条件
     *
     * @param names
     * @param condition
     */
    public static void filter(List names, Predicate condition) {
        names.stream().filter((name) -> (condition.test(name)))
                .forEach((name) -> {System.out.println(name + " ");
                });
    }



}
